/**  
* @Title: CrShopGoodsService.java
* @Package com.lemon.service.cityRun
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年10月23日
* @version V1.0  
*/
package com.lemon.service.cityRun;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.lemon.conf.content.IdTypeHandler;
import com.lemon.mapper.cityRun.CrShopActivityOutDiscountMapper;
import com.lemon.mapper.cityRun.CrShopGoodsMapper;
import com.lemon.mapper.cityRun.CrShopGoodsSpecMapper;
import com.lemon.mapper.cityRun.CrShopGoodsTypeMapper;
import com.lemon.mapper.cityRun.crShopPicMapper;
import com.lemon.model.cityRun.CrShopGoods;
import com.lemon.model.cityRun.CrShopGoodsSpec;
import com.lemon.model.cityRun.CrShopGoodsType;
import com.lemon.model.cityRun.crShopPic;
import com.lemon.utils.StringUtil;

/**
 * <p>Title: CrShopGoodsService</p>
 * <p>Description: 
 * </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年10月23日
 * @版本 V 1.0 
 */
@Service
public class CrShopGoodsServiceImpl implements CrShopGoodsService {

	@Resource
	private CrShopGoodsTypeMapper gtMapper;
	@Resource
	private CrShopGoodsMapper goMapper;
	@Resource
	private CrShopGoodsSpecMapper seMapper;
	@Resource
	private CrShopActivityOutDiscountMapper dsMapper;
	@Resource
	private crShopPicMapper picMapper;

	/**
	 * <p>
	 * Title: selectTypeListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param gt
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月23日
	 */
	@Override
	public List<CrShopGoodsType> selectTypeListBySelective(CrShopGoodsType gt) throws Exception {
		PageHelper.startPage(gt.getPageNum() , gt.getPageSize());
		List<CrShopGoodsType> list = gtMapper.selectListBySelective(gt);
		return list;
	}

	/**
	 * <p>
	 * Title: insertTypeBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param gt
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月23日
	 */
	@Override
	public int insertTypeBySelective(CrShopGoodsType gt) throws Exception {
		int selective = gtMapper.insertSelective(gt);
		return selective;
	}

	/**
	 * <p>
	 * Title: updateTypeBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param gt
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月23日
	 */
	@Override
	public int updateTypeBySelective(CrShopGoodsType gt) throws Exception {
		int selective = gtMapper.updateByPrimaryKeySelective(gt);
		return selective;
	}

	/**
	 * <p>
	 * Title: selectListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param go
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月23日
	 */
	@Override
	public List<CrShopGoods> selectListBySelective(CrShopGoods go) throws Exception {
		PageHelper.startPage(go.getPageNum() , go.getPageSize());
		List<CrShopGoods> selective = goMapper.selectListBySelective(go);
		return selective;
	}

	/**
	 * <p>
	 * Title: insertBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param gt
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月23日
	 */
	@Override
	public int insertBySelective(CrShopGoods gt) throws Exception {
		int selective = goMapper.insertSelective(gt);
		return selective;
	}

	/**
	 * <p>
	 * Title: updateBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param go
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月23日
	 */
	@Override
	public int updateBySelective(CrShopGoods go) throws Exception {
		//判断图片是否有更改
		CrShopGoods goods = goMapper.selectByPrimaryKey(go.getId());
		if (StringUtil.isNotBlank(go.getGoodsPic()) && (!go.getGoodsPic().equals(goods.getGoodsPic())
				|| !go.getGoodsPicArr().equals(goods.getGoodsPicArr()))) {
			//判断图的状态
			crShopPic pic = new crShopPic();
			pic.setPic(go.getGoodsPic());
			crShopPic pic2 = picMapper.selectBySelective(pic);
			if (pic2.getStatus() == 3) {
				go.setStatus(4);
			}
		}
		//http://qnimg.lemon-sea.com/03d3fa3b42de4165a96135cd63d2afec.jpg?imageslim
		//http://qnimg.lemon-sea.com/03d3fa3b42de4165a96135cd63d2afec.jpg?imageslim

		//
		//
		int selective = goMapper.updateByPrimaryKeySelective(go);
		return selective;
	}

	/**
	 * <p>
	 * Title: selectGoodsCountByTypeId
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param gt
	 * @return
	 * @author Ran_cc
	 * @date 2018年10月23日
	 */
	@Override
	public List<Map<String , Object>> selectGoodsCountByTypeId(CrShopGoodsType gt) {
		List<Map<String , Object>> map = goMapper.selectGoodsCountByTypeId(gt);
		return map;
	}

	/**
	 * <p>
	 * Title: selectSpecListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param se
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月25日
	 */
	@Override
	public List<CrShopGoodsSpec> selectSpecListBySelective(CrShopGoodsSpec se) throws Exception {

		return seMapper.selectListBySelective(se);
	}

	/**
	 * <p>
	 * Title: updateSpecBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param se
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月25日
	 */
	@Override
	public void updateSpecBySelective(CrShopGoodsSpec se) throws Exception {
		seMapper.updateByPrimaryKeySelective(se);
	}

	/**
	 * <p>
	 * Title: updateByPrimaryKeySelectiveByGoodsId
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param se
	 * @author Ran_cc
	 * @date 2018年10月25日
	 */
	@Override
	public void updateByPrimaryKeySelectiveByGoodsId(CrShopGoodsSpec se) throws Exception {
		seMapper.updateByPrimaryKeySelectiveByGoodsId(se);
	}

	/**
	 * <p>
	 * Title: insertSpecBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param se
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年10月25日
	 */
	@Override
	public void insertSpecBySelective(CrShopGoodsSpec se) throws Exception {
		seMapper.insertSelective(se);
	}

	/**
	 * <p>
	 * Title: selectwxListBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param go
	 * @return
	 * @throws Exception
	 * @author Ran_cc
	 * @date 2018年11月10日
	 */
	@Cacheable(cacheNames = "cityRun:wxgoods#36000#35940" , keyGenerator = "keyGenerator" , unless = "#result == null")
	@Override
	public List<CrShopGoods> selectwxListBySelective(CrShopGoods go) throws Exception {
		//		if (go.getPageSize() != 10) {
		//			PageHelper.startPage(go.getPageNum() , go.getPageSize());
		//		}
		List<CrShopGoods> list = goMapper.selectwxListBySelective(go);
		if (list.size() == 0) {
			return null;
		}
		//		CrShopActivityOutDiscount dis = null;
		CrShopGoodsSpec s = null;

		String speIds = "";

		for (int i = 0; i < list.size(); i++) {
			long ii = IdTypeHandler.decode(list.get(i).getId());
			speIds += ii;
			if ((1 + i) < list.size()) {
				speIds += ",";
			}
			/*
			 * s = new CrShopGoodsSpec();
			 * s.setGoodsId(list.get(i).getId());
			 * s.setStatus(1);
			 * List<CrShopGoodsSpec> list2 = seMapper.selectListBySelective(s);
			 * if (list != null && list.size() > 0) {
			 * list.get(i).setSpecList(list2);
			 * }
			 */
			/*
			 * String id = list.get(i).getId();
			 * dis = new CrShopActivityOutDiscount();
			 * dis.setGoodsId(id);
			 * dis.setShopId(list.get(i).getShopId());
			 * dis = dsMapper.selectwxListByACTI(dis);
			 * if (dis != null) {
			 * list.get(i).setGoodsPriceDis(dis.getGoodsPriceDis());
			 * list.get(i).setGoodsPriceDisAdd(dis.getStatus());
			 * list.get(i).setGoodsPriceDisNum(dis.getForNum());
			 * }
			 * System.out.println(11);
			 */
		}

		s = new CrShopGoodsSpec();
		s.setStatus(1);
		s.setWhere(" and a.goods_id in (" + speIds + ")");
		List<CrShopGoodsSpec> list2 = seMapper.selectListBySelective(s);
		List<CrShopGoodsSpec> specList = null;
		for (int j = 0; j < list.size(); j++) {
			specList = new ArrayList<CrShopGoodsSpec>();
			for (int i = 0; i < list2.size(); i++) {
				if (list.get(j).getId().equals(list2.get(i).getGoodsId())) {
					specList.add(list2.get(i));
				}
			}
			list.get(j).setSpecList(specList);
		}
		return list;
	}

	/**
	 * <p>
	 * Title: insertGoodsAndSpecBySelective
	 * </p>
	 * <p>
	 * Description:(一句话阐述):
	 * </p>
	 * 
	 * @param gt
	 * @param se
	 * @return
	 * @author Ran_cc
	 * @throws Exception
	 * @date 2018年11月11日
	 */
	@Transactional(rollbackFor = { Exception.class } , propagation = Propagation.REQUIRED)
	@Override
	public int insertGoodsAndSpecBySelective(CrShopGoods gt , List<CrShopGoodsSpec> seList) throws Exception {
		goMapper.insertSelective(gt);
		String string = IdTypeHandler.encode(Long.parseLong(gt.getId()));
		for (int i = 0; i < seList.size(); i++) {
			CrShopGoodsSpec se = seList.get(i);
			se.setGoodsId(string);
			se.setStatus(4);
			seMapper.insertSelective(se);
		}
		return 1;
	}

}
